package com.wolfsea.pagerecyclerviewdemo

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View

/**
 *@desc
 *@author liuliheng
 *@time 2023/1/9  22:08
 **/
class CirclePageIndicator @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : View(context, attributeSet) {

    private var itemCount = 0
    private var currentIndex = 0

    private var viewWidth = 0
    private var viewHeight = 0

    private val mContext = context

    private val circlesDistance = context.resources.getDimension(R.dimen.dp_5)
    private val horizontalSpace = context.resources.getDimension(R.dimen.dp_20).toInt()

    private val selectedPaint by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            style = Paint.Style.FILL
            color = context.resources.getColor(R.color.purple_200)
        }
    }

    private val unSelectedPaint by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            style = Paint.Style.FILL
            color = context.resources.getColor(R.color.purple_500)
        }
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)

        viewWidth = when (MeasureSpec.getMode(widthMeasureSpec)) {
            MeasureSpec.EXACTLY -> {
                MeasureSpec.getSize(widthMeasureSpec)
            }
            else -> {
                mContext.resources.getDimension(R.dimen.dp_300).toInt()
            }
        } - paddingStart - paddingEnd

        viewHeight = when (MeasureSpec.getMode(heightMeasureSpec)) {
            MeasureSpec.EXACTLY -> {
                MeasureSpec.getSize(heightMeasureSpec)
            }
            else -> {
                mContext.resources.getDimension(R.dimen.dp_45).toInt()
            }
        } - paddingTop - paddingBottom

        setMeasuredDimension(viewWidth, viewHeight)
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        if (itemCount > 1) {
            canvas?.apply {
                val cy = viewHeight.toFloat() / 2
                for (i in 0.until(itemCount)) {
                   drawCircle(
                        calculatePointIndicatorX(i),
                        cy,
                        circlesDistance,
                        if (currentIndex == i) selectedPaint else unSelectedPaint
                    )
                }
            }
        }
    }

    /*计算圆形指示器的x轴坐标*/
    private fun calculatePointIndicatorX(pageIndex: Int): Float {
        return if (itemCount % 2 == 0) {
            val middleValue = itemCount / 2
            val space = when {
                pageIndex == middleValue - 1 -> {
                    -horizontalSpace / 2
                }
                pageIndex == middleValue -> {
                    horizontalSpace / 2
                }
                pageIndex < middleValue - 1 -> {
                    (pageIndex - (middleValue - 1)) * horizontalSpace - horizontalSpace / 2
                }
                pageIndex > middleValue -> {
                    (pageIndex - middleValue) * horizontalSpace + horizontalSpace / 2
                }
                else -> {
                    0
                }
            }
            viewWidth.toFloat() / 2 + space
        } else {
            viewWidth.toFloat() / 2 + (pageIndex - itemCount / 2) * horizontalSpace
        }
    }

    fun setItemCount(itemCount: Int) {
        this.itemCount = itemCount
        invalidate()
    }

    fun updateIndex(index: Int) {
        currentIndex = index
        invalidate()
    }

}